package com.i2works.smartluolong.web.service.flow.impl;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.utils.base.GlobalConstant;
import com.i2works.smartluolong.utils.basecommon.BaseCommonServiceImpl;
import com.i2works.smartluolong.utils.common.WebUtil;
import com.i2works.smartluolong.utils.exception.DAOException;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.dal.flow.dao.IFlowInfoDAO;
import com.i2works.smartluolong.web.entity.flow.FlowInfoDO;
import com.i2works.smartluolong.web.entity.flow.FlowInfoQuery;
import com.i2works.smartluolong.web.entity.flow.FlowProcessDO;
import com.i2works.smartluolong.web.service.flow.IFlowInfoService;
import com.i2works.smartluolong.web.service.flow.IFlowProcessService;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Liu Yapeng
 * @date 2018-14-110 14:37:02
 */
@Service("flowInfoService")
public class FlowInfoServiceImpl extends BaseCommonServiceImpl<FlowInfoQuery, FlowInfoDO> implements IFlowInfoService {
    @Autowired
    private IFlowInfoDAO flowInfoDAO;

    @Autowired
    private IFlowProcessService flowProcessService;

    /**
     * 根据item_id和item_name查询更新
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void updateByItem(FlowInfoDO flowInfoDO) throws ServiceException {
        try {
            flowInfoDAO.updateByItem(flowInfoDO);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }

    }


    /**
     * 更新工作流
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void save(FlowInfoDO flowInfo) throws ServiceException {
        try {
            FlowInfoQuery flowInfoQuery = new FlowInfoQuery();
            flowInfoQuery.setItemId(flowInfo.getItemId());
            flowInfoQuery.setUserId(flowInfo.getUserId());
            flowInfoQuery.setItemType(flowInfo.getItemType());
            FlowInfoDO resultFlowInfo = flowInfoDAO.query(flowInfoQuery);
            if (null != resultFlowInfo) {
                flowInfoDAO.delete(resultFlowInfo.getId());
            }
            flowInfoDAO.save(flowInfo);
            //添加工作流日志
            FlowProcessDO flowProcessDO = new FlowProcessDO();
            flowProcessDO.setItemId(flowInfo.getItemId());
            flowProcessDO.setItemTitle(flowInfo.getItemName());
            flowProcessDO.setItemType(flowInfo.getItemType());
            flowProcessDO.setMessage(flowInfo.getMessage());
            flowProcessDO.setAuditStatus(flowInfo.getItemStatus());
            flowProcessDO.setCreateName(flowInfo.getUpdateName());
            flowProcessDO.setUpdateName(flowInfo.getUpdateName());
            flowProcessService.save(flowProcessDO);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }

    }

    /**
     * 获取监控报表事项办理列表
     */
    @Override
    public List<FlowInfoDO> queryListByMonitor(FlowInfoQuery query) throws ServiceException {
        List<FlowInfoDO> resultList = Lists.newArrayList();
        try {
            List<FlowInfoDO> infoList = flowInfoDAO.queryListByMonitor(query);
            if (CollectionUtil.isNotEmpty(infoList)) {
                for (FlowInfoDO flowInfoDO : infoList) {
                    //查询事项类型
                    String itemType = flowInfoDO.getItemType();
                    //工商
                    if (itemType.startsWith(GlobalConstant.itemsPrefix.COMPANY)) {
                        flowInfoDO.setItemStatusText(WebUtil.companyItemStatus(flowInfoDO.getItemStatus()));
                    }
                    //食药
                    if (itemType.startsWith(GlobalConstant.itemsPrefix.FOODDRUG)) {
                        flowInfoDO.setItemStatusText(WebUtil.fooddrugItemStatus(flowInfoDO.getItemStatus()));
                    }
                    //残联
                    if (itemType.startsWith(GlobalConstant.itemsPrefix.DISABILITY)) {
                        flowInfoDO.setItemStatusText(WebUtil.disabilityItemStatus(flowInfoDO.getItemStatus()));
                    }
                    flowInfoDO.setItemTypeText(WebUtil.itemTypeValue(flowInfoDO.getItemType()));
                    resultList.add(flowInfoDO);
                }
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
        return resultList;
    }


    /**
     * 事项办理报表
     */
    @Override
    public List<FlowInfoDO> queryItemReport(FlowInfoQuery query) throws ServiceException {
        try {
            List<FlowInfoDO> list = flowInfoDAO.queryItemReport(query);
            if (CollectionUtil.isNotEmpty(list)) {
                List<FlowInfoDO> resultList = Lists.newArrayList();
                for (FlowInfoDO flowInfoDO : list) {
                    flowInfoDO.setItemTypeText(WebUtil.itemTypeValue(flowInfoDO.getItemType()));
                    resultList.add(flowInfoDO);
                }
                return resultList;
            } else {
                return null;
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


}
